package com.myapp.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.myapp.foodapp.bean.LoginBean;
import com.myapp.foodapp.net.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";
    EditText mEtTelephone,mEtPassword;
    Button mBtnLogin,mBtnCancel;
    TextView mTvRegister,mTvForgetPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initEvent();
    }

    private void initEvent() {
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String telephone = mEtTelephone.getText().toString();
                final String password = mEtPassword.getText().toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("telephone",telephone);
                            jsonObject.put("password",password);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String url = "http://10.2.64.75:8080/foodserver/user/login";

                        String data = NetUtil.getData(jsonObject, url);

                        Gson gson = new Gson();
                            final LoginBean loginBean = gson.fromJson(data, LoginBean.class);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if(loginBean.isSuccess()){
                                        Intent intent = new Intent(LoginActivity.this,
                                                FoodListActivity.class);
                                        //获取用户ID，传递给下一个Activity
                                        int id = loginBean.getData().getId();
//                                        intent.putExtra("userId",id);
                                        //将suerId存放到共享存储空间中
                                    SharedPreferences sp = getSharedPreferences("data.sp",MODE_PRIVATE);
                                    SharedPreferences.Editor editor = sp.edit();
                                    editor.putInt("userId",id);
                                    editor.commit();
                                        startActivity(intent);
                                    }else{
                                        Toast.makeText(LoginActivity.this,
                                                "用户名密码错误！",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });


                    }
                }).start();
            }
        });
    }


    private void initView() {
        mEtTelephone = findViewById(R.id.et_telephone);
        mEtPassword = findViewById(R.id.et_password);
        mBtnLogin = findViewById(R.id.btn_login);
        mBtnCancel = findViewById(R.id.btn_cancel);
        mTvRegister = findViewById(R.id.tv_register);
        mTvForgetPassword = findViewById(R.id.tv_forget_password);
    }
}